package org.ben.gather;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: Ben
 * @Date: 2019/10/16 22:12
 */
public class MapTest {
    public static void main(String[] args) {
        HashMap<Integer,String> hashMapIntegerString=new HashMap<>();
        hashMapIntegerString.put(1,"Java");
        hashMapIntegerString.put(2,"Pyhton");
        hashMapIntegerString.put(3,"C");
        hashMapIntegerString.put(4,"C++");
        hashMapIntegerString.put(5,"Matlab");
        hashMapIntegerString.put(6,"R");
        hashMapIntegerString.put(7,"html");
        hashMapIntegerString.put(8,"CSS");
        hashMapIntegerString.put(9,"JavaScript");
        MapTest mapTest=new MapTest();
        mapTest.forIntegerStringMap(hashMapIntegerString);
        HashMap<Integer,Integer> hashMapIntegerInteger=new HashMap<>();
        for (int i = 0; i <10 ; i++) {
            hashMapIntegerInteger.put(i,i);
        }
        mapTest.forEachIntegerIntegerkeySetMap(hashMapIntegerInteger);
        System.out.println("--------------------------------------------------");
        mapTest.forEachIntegerIntegerkeySetValuestMap(hashMapIntegerInteger);
        System.out.println("===================================");
        mapTest.forEachEntryIntegerIntegerMap(hashMapIntegerInteger);
        HashMap<Integer,User> hashMapIntegerUser=new HashMap<>();
        hashMapIntegerUser.put(1,new User(1,"mjz","mjz","mjz","mjz"));
        hashMapIntegerUser.put(2,new User(2,"aaa","aaa","aaa","aaa"));
        hashMapIntegerUser.put(3,new User(3,"bbb","bbb","bbb","bbb"));
        hashMapIntegerUser.put(4,new User(4,"ccc","ccc","ccc","ccc"));
        hashMapIntegerUser.put(5,new User(5,"ddd","ddd","ddd","ddd"));
        hashMapIntegerUser.put(6,new User(6,"eee","eee","eee","eee"));
        hashMapIntegerUser.put(7,new User(7,"fff" ,"fff","fff","fff"));
        System.out.println("-------------------------------------------");
        mapTest.IteratorKeySetIntegerUserMap(hashMapIntegerUser);
        System.out.println("===========================================");
        mapTest.IteratorEntrySetIntegerUserMap(hashMapIntegerUser);
    }

    /**
     * 用for循环迭代HashMap<Integer,String>集合
     * @param hasMap
     */
    public void forIntegerStringMap(HashMap<Integer,String> hasMap){
        for (int i = 1; i <=hasMap.size() ; i++) {
            System.out.println(hasMap.get(i));
        }
    }

    /**
     * 用foreach循环并结合keySet结合get方法迭代HashMap<Integer,Integer>集合
     * @param hasMap
     */
    public void forEachIntegerIntegerkeySetMap(HashMap<Integer,Integer> hasMap){
    for(Integer s:hasMap.keySet()){
                    System.out.println("key : "+s+" value : "+hasMap.get(s));
               }
}

    /**
     * 用foreach循环并结合keySet和values方法迭代HashMap<Integer,Integer>集合
     * @param hasMap
     */
    public  void forEachIntegerIntegerkeySetValuestMap(HashMap<Integer,Integer> hasMap){
    for(Integer s1:hasMap.keySet()){//遍历map的键
                  System.out.println("键key ："+s1);
               }
          for(Integer s2:hasMap.values()){//遍历map的值
                    System.out.println("值value ："+s2);
              }
}

    /**
     * 用foreach循环并结合entrySet方法迭代HashMap<Integer,Integer>集合
     * @param hasMap
     */
    public  void forEachEntryIntegerIntegerMap(HashMap<Integer,Integer> hasMap){
        for(Map.Entry<Integer,Integer> entry : hasMap.entrySet()){
                   System.out.println("键 key ："+entry.getKey()+" 值value ："+entry.getValue());
                }
}

    /**
     * 用Iterator迭代器并调用keySet方法迭代HashMap<Integer,User>集合
     * @param hasMap
     */
    public void IteratorKeySetIntegerUserMap(HashMap<Integer,User> hasMap){
    Iterator<Integer> integerIterator = hasMap.keySet().iterator();//有了Set集合，就可以获取其迭代器。

 while(integerIterator.hasNext()){
 Integer key = integerIterator.next();
 User value = hasMap.get(key);//有了键可以通过map集合的get方法获取其对应的值。
 System.out.println("key: "+key+"-->value: "+value.toString());//获得key和value值
}

}

    /**
     * 用Iterator迭代器并结合EntrySet方法迭代HashMap<Integer,User>集合
     * @param hasMap
     */
    public  void IteratorEntrySetIntegerUserMap(HashMap<Integer,User> hasMap){
    Iterator<Map.Entry<Integer, User>> entryIterator = hasMap.entrySet().iterator();
    while(entryIterator.hasNext()){
 Map.Entry<Integer, User> me = entryIterator.next();//获取Map.Entry关系对象me
        Integer key2 = me.getKey();//通过关系对象获取key
        User value2 = me.getValue();//通过关系对象获取value
 System.out.println("key: "+key2+"-->value: "+value2.toString());
    }
}
}
